package com.kevin.mapping.script.node.op;

import com.kevin.mapping.script.engine.JSONEnv;
import com.kevin.mapping.script.exception.ExecuteException;
import com.kevin.mapping.script.lexer.*;
import com.kevin.mapping.script.node.stmt.Stmt;

public class Minus extends Stmt {

    private Stmt left;
    private Stmt right;


    public Minus(Token token, Stmt left, Stmt right) {
        super(token);
        this.left = left;
        this.right = right;
    }

    public Stmt getLeft() {
        return left;
    }

    public void setLeft(Stmt left) {
        this.left = left;
    }

    public Stmt getRight() {
        return right;
    }

    public void setRight(Stmt right) {
        this.right = right;
    }

    @Override
    public Token execute(JSONEnv env) {
        Token leftRet = left.execute(env);
        Token rightRet = right.execute(env);
        int type = Math.max(leftRet.type, rightRet.type);
        switch (type) {
            case Type.INTEGER:
                return new IntegerValue(Type.INTEGER, leftRet.getIntValue(env) - rightRet.getIntValue(env));
            case Type.LONG:
                return new LongValue(Type.LONG, leftRet.getLongValue(env) - rightRet.getLongValue(env));
            case Type.DOUBLE:
                return new DoubleValue(Type.DOUBLE, leftRet.getDoubleValue(env) - rightRet.getDoubleValue(env));
            case Type.BIG_DECIMAL:
                return new StrValue(Type.STR_VALUE, leftRet.getBigDecimalValue(env).subtract(rightRet.getBigDecimalValue(env)).toPlainString());
            default:
                throw new ExecuteException("unsuported type" + type);
        }

    }
}
